import paramiko

# 链接服务器的上下文实例


class Orin(object):
    def __init__(self, ip=None, username="", password="", type=""):
        self.ip = ip
        self.username = username
        self.password = password
        self.port = 22
        self.type = type

    def __enter__(self):
        """上文函数"""
        # 创建ssh链接对象并返回
        self.ssh = paramiko.SSHClient()
        self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        self.ssh.connect(self.ip, port=self.port, username=self.username, password=self.password)

        return self.ssh

    def __exit__(self, exc_type, exc_val, exc_tb):
        """下文函数"""
        # 关闭链接
        self.ssh.close()
